use criterion::{black_box, criterion_group, criterion_main, Criterion};
use crate::slist::*;
fn slist_benchmark(c: &mut Criterion) {
    let mut list: Option<Box<SListEntry<i32>>> = None;

    // Benchmark for slist_prepend
    c.bench_function("slist_slist_prepend", |b| {
        b.iter(|| {
            for i in 0..1000 {
                slist_prepend(&mut list, black_box(i));
            }
        })
    });

    // Benchmark for slist_append
    c.bench_function("slist_slist_append", |b| {
        b.iter(|| {
            for i in 0..1000 {
                slist_append(&mut list, black_box(i));
            }
        })
    });

    // Benchmark for slist_nth_entry
    c.bench_function("slist_slist_nth_entry", |b| {
        b.iter(|| {
            for i in 0..1000 {
                slist_nth_entry(&list, black_box(i % 1000));
            }
        })
    });

    // Benchmark for slist_nth_data
    c.bench_function("slist_slist_nth_data", |b| {
        b.iter(|| {
            for i in 0..1000 {
                slist_nth_data(&list, black_box(i % 1000));
            }
        })
    });

    // Benchmark for slist_length
    c.bench_function("slist_slist_length", |b| {
        b.iter(|| {
            slist_length(&list);
        })
    });

    // Benchmark for slist_to_array
    c.bench_function("slist_slist_to_array", |b| {
        b.iter(|| {
            slist_to_array(&list);
        })
    });

    // Benchmark for slist_remove_data
    c.bench_function("slist_slist_remove_data", |b| {
        b.iter(|| {
            slist_remove_data(&mut list, |a, b| if a == b { 1 } else { 0 }, &black_box(500));
        })
    });

    // Benchmark for slist_remove_entry
    c.bench_function("slist_slist_remove_entry", |b| {
        b.iter(|| {
            let entry_ptr = slist_nth_entry(&list, 500).map(|entry| entry as *const _);
            if let Some(ptr) = entry_ptr {
                slist_remove_entry(&mut list, ptr);
            }
        })
    });

    // Benchmark for slist_find_data
    c.bench_function("slist_slist_find_data", |b| {
        b.iter(|| {
            slist_find_data(&list, |a, b| if a == b { 1 } else { 0 }, &black_box(500));
        })
    });

    // Benchmark for slist_sort
    c.bench_function("slist_slist_sort", |b| {
        b.iter(|| {
            slist_sort(&mut list, |a, b| a.cmp(b));
        })
    });

    // Benchmark for slist_free
    c.bench_function("slist_slist_free", |b| {
        b.iter(|| {
            slist_free(&mut list);
        })
    });
}

criterion_group!(benches, slist_benchmark);
criterion_main!(benches);